'use strict';

goog.provide('TCR.FieldTextInput');
goog.require('Blockly.Blocks');
goog.require('goog.ui.Dialog')
goog.require('goog.ui.Slider');
var lastShowDialog =new Date();
TCR.FieldTextInput = function(text) {
	TCR.FieldTextInput.superClass_.constructor.call(this, text);
};
goog.inherits(TCR.FieldTextInput, Blockly.FieldTextInput);

TCR.FieldTextInput.prototype.showEditor_ = function(opt_quietInput) {
	return;
}

TCR.FieldTextInput.prototype.setValue = function(text) {
	if (text === null) {
    return;
}
if (this.sourceBlock_) {
	var validated = this.callValidator(text);
    if (validated !== null) {
    	text = validated;
    }
}
	Blockly.FieldTextInput.prototype.setValue.call(this, text);
};

//关节点移动
Blockly.Blocks['tcr_j_fine'] = {
	init: function() {
		this.appendDummyInput()
			.appendField("移动到点")
			.appendField(new TCR.FieldTextInput("1"), "j1")
			.appendField("速度")
			.appendField(new TCR.FieldTextInput("100"), "j2")
			.appendField("%");
		this.setPreviousStatement(true, null);
		this.setNextStatement(true, null);
		this.setColour(230);
		this.setTooltip('');
		this.setHelpUrl('');
		this.contextMenu = true;
	},
	onchange: function(ev) {
		if(ev.type == Blockly.Events.UI && ev.element == 'click' && this.id == ev.blockId) {
			var block = this;
			var dialog = new goog.ui.Dialog();
			//lhm 补充id = 3d_view 2017.07.21
			dialog.setContent(
				'<div id="futitle" style="position: absolute;top: 20%;left:0%;display: none;">关节移动到点<span id="dian" style="color:#00FFFF;">1</span>&nbsp;速度<span id="speed" style="color:#00FFFF;">100</span>%</div>'+
				
				'<div id="3d_view" style="position: absolute; left: 0%; top: 18%; width: 45%; height: 60%;text-align: center;border:1px solid #fff;"></div>' +
				
				'<div style="position:absolute;left: 69%;top: 22%;color:#fff;">点位</div>'+
				'<div id ="numdian" style="display:block; width: 67.2%; height: 70%; position: absolute; left: 47%; top: 12%; background: transparent;">' +
				'<div style="font-size: 32px;position: absolute; left: 0%; top: 25%; width: 15%; height: 15%;text-align: center;">1</div>' +
				'<div style="font-size: 32px;position: absolute; left: 15%; top: 25%; width: 15%; height: 15%;text-align: center;">2</div>' +
				'<div style="font-size: 32px;position: absolute; left: 30%; top: 25%; width: 15%; height: 15%;text-align: center;">3</div>' +
				'<div style="font-size: 32px;position: absolute; left: 45%; top: 25%; width: 15%; height: 15%;text-align: center;">4</div>' +
				'<div style="font-size: 32px;position: absolute; left: 60%; top: 25%; width: 15%; height: 15%;text-align: center;">5</div>' +
				'<div style="font-size: 32px;position: absolute; left: 0%; top: 40%; width: 15%; height: 15%;text-align: center;">6</div>' +
				'<div style="font-size: 32px;position: absolute; left: 15%; top: 40%; width: 15%; height: 15%;text-align: center;">7</div>' +
				'<div style="font-size: 32px;position: absolute; left: 30%; top: 40%; width: 15%; height: 15%;text-align: center;">8</div>' +
				'<div style="font-size: 32px;position: absolute; left: 45%; top: 40%; width: 15%; height: 15%;text-align: center;">9</div>' +
				'<div style="font-size: 32px;position: absolute; left: 60%; top: 40%; width: 15%; height: 15%;text-align: center;">10</div>' +
				'</div>' +
				
				'<div style="position:absolute;left: 68%;top: 54%;color:#fff;">速度<span id="speed2" style="color:#00FFFF;">100</span><span style="color:#00FFFF;">%</span></div>'+
				'<input id="move_speed" type="range" style="position:absolute;left: 52%;top: 62%;" class="move_speed"/>'+
				
				'<div id="cancelsruebut"><span id="cancelbut">取消</span><span id="sruebut">确定</span></div>'
				);
			
			dialog.setHasTitleCloseButton(false);
			dialog.setDraggable(false);
			dialog.setVisible(true);
			dialog.setDisposeOnHide(true);
			//lhm 补充 添加3d显示 2017.07.22
			//init_3d();
			//animate_3d();
			
			//保存当前的点位和速度的值
			var a = block.getFieldValue('j1');
			var b = block.getFieldValue('j2');
			//设置点位
			var numdian = document.getElementById('numdian').getElementsByTagName('div');
			var dian = document.getElementById('dian');
			dian.innerHTML = block.getFieldValue('j1');
			$('#numdian div').eq(dian.innerHTML-1).css('color','#00C5AC');
			for(var i = 0; i < numdian.length; i++) {
				numdian[i].index = i;
				numdian[i].onclick=function() {
					block.setFieldValue(numdian_string[this.index], 'j1');
					dian.innerHTML = block.getFieldValue('j1');
					$('#numdian div').eq(this.index).css('color','#00C5AC').siblings().css('color','#fff');
				}
			}
			
			//设置速度
			var con;
			con=block.getFieldValue('j2');
			$("#move_speed").css("background-size",con+"%"+" "+100+"%");
			$("#speed").html(con);
			$("#speed2").html(con);
			$('#move_speed')[0].value=con;
			$('#move_speed').on('input propertychange',function(){
			 	con=$('#move_speed').val();
		        $("#move_speed").css("background-size",con+"%"+" "+100+"%");
		        $("#speed").html(con);
		        $("#speed2").html(con);
		        block.setFieldValue(con, 'j2');
		    });
		    
			//取消设定的值，还原到原来的值
			$("#cancelbut").on('click',function(){
				block.setFieldValue(a, 'j1');
				block.setFieldValue(b, 'j2');
				$(".modal-dialog-bg").remove();
				$(".modal-dialog").remove();
			});
			//确定
			$("#sruebut").on('click',function(){
				$(".modal-dialog-bg").remove();
				$(".modal-dialog").remove();
			});
		}
	}
};
Blockly.JavaScript['tcr_j_fine'] = function(a) {
	var b = a.getFieldValue('j1');
	var c = a.getFieldValue('j2');
	var code = "J P[" + b + "] " + c + "% FINE;";
	return code;
};
//关节平滑移动
Blockly.Blocks['tcr_j_cnt'] = {
	init: function() {
		this.appendDummyInput()
			.appendField("平滑移动到点")
			.appendField(new TCR.FieldTextInput("1"), "j1")
			.appendField("速度")
			.appendField(new TCR.FieldTextInput("100"), "j2")
			.appendField("%")
			.appendField("平滑度")
			.appendField(new TCR.FieldTextInput("50"), "j3")
			.appendField("%");
		this.setPreviousStatement(true, null);
		this.setNextStatement(true, null);
		this.setColour(230);
		this.setTooltip('');
		this.setHelpUrl('');
		this.contextMenu = false;
	},
	onchange: function(ev) {
		if(ev.type == Blockly.Events.UI && ev.element == 'click' && this.id == ev.blockId) {
			var block = this;
			var dialog = new goog.ui.Dialog();
			dialog.setContent(
				'<div id="futitle" style="position: absolute;top: 12%;left:0%;display: none;">关节平滑移动到点<span id="dian1" style="color:#00FFFF;">1</span>&nbsp;速度<span id="speed1" style="color:#00FFFF;">100</span>%&nbsp;平滑度<span id="cut1" style="color:#00FFFF;">100</span>%</div>'+
				
				'<div style="position:absolute;left: 49%;top: 22%;color:#fff;">点位</div>'+
				'<div id ="numdian" style="display:block; width: 70%; height: 70%; position: absolute; left: 24%; top: 20%; background: transparent;">' +
				'<div style="font-size: 32px;position: absolute; left: 0%; top: 12%; width: 15%; height: 15%;text-align: center;">1</div>' +
				'<div style="font-size: 32px;position: absolute; left: 15%; top: 12%; width: 15%; height: 15%;text-align: center;">2</div>' +
				'<div style="font-size: 32px;position: absolute; left: 30%; top: 12%; width: 15%; height: 15%;text-align: center;">3</div>' +
				'<div style="font-size: 32px;position: absolute; left: 45%; top: 12%; width: 15%; height: 15%;text-align: center;">4</div>' +
				'<div style="font-size: 32px;position: absolute; left: 60%; top: 12%; width: 15%; height: 15%;text-align: center;">5</div>' +
				'<div style="font-size: 32px;position: absolute; left: 0%; top: 28%; width: 15%; height: 15%;text-align: center;">6</div>' +
				'<div style="font-size: 32px;position: absolute; left: 15%; top: 28%; width: 15%; height: 15%;text-align: center;">7</div>' +
				'<div style="font-size: 32px;position: absolute; left: 30%; top: 28%; width: 15%; height: 15%;text-align: center;">8</div>' +
				'<div style="font-size: 32px;position: absolute; left: 45%; top: 28%; width: 15%; height: 15%;text-align: center;">9</div>' +
				'<div style="font-size: 32px;position: absolute; left: 60%; top: 28%; width: 15%; height: 15%;text-align: center;">10</div>' +
				'</div>' +
				
				'<div style="position:absolute;left: 48%;top: 50%;color:#fff;">速度<span id="speed2" style="color:#00FFFF;">100</span><span style="color:#00FFFF;">%</span></div>'+
				'<input id="move_speed" type="range" style="position:absolute;left: 22%;top: 58%;"/>'+
				
				'<div style="position:absolute;left: 47%;top: 65%;color:#fff;">平滑度<span id="cut2" style="color:#00FFFF;">50</span><span style="color:#00FFFF;">%</span></div>'+
				'<input id="move_cut" type="range" style="position:absolute;left: 22%;top: 73%;"/>'+
				
				'<div id="cancelsruebut"><span id="cancelbut">取消</span><span id="sruebut">确定</span></div>'
			);
			dialog.setHasTitleCloseButton(false);
			dialog.setDraggable(false);
			dialog.setVisible(true);
			dialog.setDisposeOnHide(true);
			//保存当前的点位和速度的值
			var a = block.getFieldValue('j1');
			var b = block.getFieldValue('j2');
			var c = block.getFieldValue('j3');
			//设置点位
			var numdian = document.getElementById('numdian').getElementsByTagName('div');
			var dian1 = document.getElementById('dian1');
			dian1.innerHTML = block.getFieldValue('j1');
			$('#numdian div').eq(dian1.innerHTML-1).css('color','#00C5AC');
			for(var i = 0; i < numdian.length; i++) {
				numdian[i].index = i;
				numdian[i].onclick=function() {
					block.setFieldValue(numdian_string[this.index], 'j1');
					dian1.innerHTML = block.getFieldValue('j1');
					$('#numdian div').eq(this.index).css('color','#00C5AC').siblings().css('color','#fff');
				}
			}
			//设置速度
			var con;
			con=block.getFieldValue('j2');
			$("#move_speed").css("background-size",con+"%"+" "+100+"%");
			$("#speed1").html(con);
			$("#speed2").html(con);
			$('#move_speed')[0].value=con;
			$('#move_speed').on('input propertychange',function(){
			 	con=$('#move_speed').val();
		        $("#move_speed").css("background-size",con+"%"+" "+100+"%");
		        $("#speed1").html(con);
		        $("#speed2").html(con);
		        block.setFieldValue(con, 'j2');
		    });
		    //设置平滑度
			var ccn;
			ccn=block.getFieldValue('j3');
			$("#move_cut").css("background-size",ccn+"%"+" "+100+"%");
			$("#cut1").html(ccn);
			$("#cut2").html(ccn);
			$('#move_cut')[0].value=ccn;
			$('#move_cut').on('input propertychange',function(){
			 	ccn=$('#move_cut').val();
		        $("#move_cut").css("background-size",ccn+"%"+" "+100+"%");
		        $("#cut1").html(ccn);
		        $("#cut2").html(ccn);
		        block.setFieldValue(ccn, 'j3');
		    });
			//取消设定的值，还原到原来的值
			$("#cancelbut").on('click',function(){
				block.setFieldValue(a, 'j1');
				block.setFieldValue(b, 'j2');
				block.setFieldValue(c, 'j3');
				$(".modal-dialog-bg").remove();
				$(".modal-dialog").remove();
			});
			//确定
			$("#sruebut").on('click',function(){
				$(".modal-dialog-bg").remove();
				$(".modal-dialog").remove();
			});
			
		}
	}
};
Blockly.JavaScript['tcr_j_cnt'] = function(block) {
	var b = block.getFieldValue('j1');
	var c = block.getFieldValue('j2');
	var d = block.getFieldValue('j3');
	var code = "J P[" + b + "] " + c + "% CNT" + d + ";";
	return code;
};

//直线移动
Blockly.Blocks['tcr_l_fine'] = {
	init: function() {
		this.appendDummyInput()
			.appendField("直线移动到点")
			.appendField(new TCR.FieldTextInput("1"), "l1")
			.appendField("速度")
			.appendField(new TCR.FieldTextInput("100"), "l2")
			.appendField("%");
		this.setPreviousStatement(true, null);
		this.setNextStatement(true, null);
		this.setColour(230);
		this.setTooltip('');
		this.setHelpUrl('');
		this.contextMenu = false;
	},
	onchange: function(ev) {
		if(ev.type == Blockly.Events.UI && ev.element == 'click' && this.id == ev.blockId) {
			var block = this;
			var dialog = new goog.ui.Dialog();
			dialog.setContent(
				'<div id="futitle" style="position: absolute;top: 20%;left:0%;display: none;">直线移动到点<span id="dian" style="color:#00FFFF;">1</span>&nbsp;速度<span id="speed" style="color:#00FFFF;">100</span>%</div>'+
				
				'<div style="position:absolute;left: 49%;top: 22%;color:#fff;">点位</div>'+
				'<div id ="numdian" style="display:block; width: 70%; height: 70%; position: absolute; left: 24%; top: 12%; background: transparent;">' +
				'<div style="font-size: 32px;position: absolute; left: 0%; top: 25%; width: 15%; height: 15%;text-align: center;">1</div>' +
				'<div style="font-size: 32px;position: absolute; left: 15%; top: 25%; width: 15%; height: 15%;text-align: center;">2</div>' +
				'<div style="font-size: 32px;position: absolute; left: 30%; top: 25%; width: 15%; height: 15%;text-align: center;">3</div>' +
				'<div style="font-size: 32px;position: absolute; left: 45%; top: 25%; width: 15%; height: 15%;text-align: center;">4</div>' +
				'<div style="font-size: 32px;position: absolute; left: 60%; top: 25%; width: 15%; height: 15%;text-align: center;">5</div>' +
				'<div style="font-size: 32px;position: absolute; left: 0%; top: 40%; width: 15%; height: 15%;text-align: center;">6</div>' +
				'<div style="font-size: 32px;position: absolute; left: 15%; top: 40%; width: 15%; height: 15%;text-align: center;">7</div>' +
				'<div style="font-size: 32px;position: absolute; left: 30%; top: 40%; width: 15%; height: 15%;text-align: center;">8</div>' +
				'<div style="font-size: 32px;position: absolute; left: 45%; top: 40%; width: 15%; height: 15%;text-align: center;">9</div>' +
				'<div style="font-size: 32px;position: absolute; left: 60%; top: 40%; width: 15%; height: 15%;text-align: center;">10</div>' +
				'</div>' +
				
				'<div style="position:absolute;left: 48%;top: 55%;color:#fff;">速度<span id="speed2" style="color:#00FFFF;">100</span><span style="color:#00FFFF;">%</span></div>'+
				'<input id="move_speed" type="range" style="position:absolute;left: 22%;top: 62%;"/>'+
				
				'<div id="cancelsruebut"><span id="cancelbut">取消</span><span id="sruebut">确定</span></div>'
				
			);
			dialog.setHasTitleCloseButton(false);
			dialog.setDraggable(false);
			dialog.setVisible(true);
			dialog.setDisposeOnHide(true);
			
			//保存当前的点位和速度的值
			var a = block.getFieldValue('l1');
			var b = block.getFieldValue('l2');
			//设置点位
			var numdian = document.getElementById('numdian').getElementsByTagName('div');
			var dian = document.getElementById('dian');
			dian.innerHTML = block.getFieldValue('l1');
			$('#numdian div').eq(dian.innerHTML-1).css('color','#00C5AC');
			for(var i = 0; i < numdian.length; i++) {
				numdian[i].index = i;
				numdian[i].onclick=function() {
					block.setFieldValue(numdian_string[this.index], 'l1');
					dian.innerHTML = block.getFieldValue('l1');
					$('#numdian div').eq(this.index).css('color','#00C5AC').siblings().css('color','#fff');
				}
			}
			//设置速度
			var con;
			con=block.getFieldValue('l2');
			$("#move_speed").css("background-size",con+"%"+" "+100+"%");
			$("#speed").html(con);
			$("#speed2").html(con);
			$('#move_speed')[0].value=con;
			$('#move_speed').on('input propertychange',function(){
			 	con=$('#move_speed').val();
		        $("#move_speed").css("background-size",con+"%"+" "+100+"%");
		        $("#speed").html(con);
		        $("#speed2").html(con);
		        block.setFieldValue(con, 'l2');
		    });
			//取消设定的值，还原到原来的值
			$("#cancelbut").on('click',function(){
				block.setFieldValue(a, 'l1');
				block.setFieldValue(b, 'l2');
				$(".modal-dialog-bg").remove();
				$(".modal-dialog").remove();
			});
			//确定
			$("#sruebut").on('click',function(){
				$(".modal-dialog-bg").remove();
				$(".modal-dialog").remove();
			});
		}
	}
};
Blockly.JavaScript['tcr_l_fine'] = function(block) {
	var b = block.getFieldValue('l1');
	var c = block.getFieldValue('l2');
	var code = "L P[" + b + "] " + c + "% FINE;";
	return code;
};
//圆弧移动
Blockly.Blocks['tcr_c'] = {
	init: function() {
		this.appendDummyInput()
			.appendField("圆弧移动到过渡点")
			.appendField(new TCR.FieldTextInput("1"), "c1")
			.appendField("结束点")
			.appendField(new TCR.FieldTextInput("5"), "c2")
			.appendField("速度")
			.appendField(new TCR.FieldTextInput("100"), "c3")
			.appendField("%");
		this.setPreviousStatement(true, null);
		this.setNextStatement(true, null);
		this.setColour(230);
		this.setTooltip('');
		this.setHelpUrl('');
		this.contextMenu = false;
	},
	onchange: function(ev) {
		if(ev.type == Blockly.Events.UI && ev.element == 'click' && this.id == ev.blockId) {
			var block = this;
			var dialog = new goog.ui.Dialog();
			dialog.setContent(
				'<div id="futitle" style="position: absolute;top: 7%;left:0%;display: none;">圆弧移动到过渡点<span id="dian" style="color:#00FFFF;">1</span>&nbsp;结束点<span id="over" style="color:#00FFFF;">5</span>&nbsp;速度<span id="speed" style="color:#00FFFF;">100</span>%</div>'+
				
				'<div style="position:absolute;left: 47%;top: 12%;color:#fff;">过渡点</div>'+
				'<div id ="numdian" style="display:block; width: 70%; height: 70%; position: absolute; left: 24%; top: 0%; background: transparent;">' +
				'<div style="font-size: 32px;position: absolute; left: 0%; top: 25%; width: 15%; height: 15%;text-align: center;">1</div>' +
				'<div style="font-size: 32px;position: absolute; left: 15%; top: 25%; width: 15%; height: 15%;text-align: center;">2</div>' +
				'<div style="font-size: 32px;position: absolute; left: 30%; top: 25%; width: 15%; height: 15%;text-align: center;">3</div>' +
				'<div style="font-size: 32px;position: absolute; left: 45%; top: 25%; width: 15%; height: 15%;text-align: center;">4</div>' +
				'<div style="font-size: 32px;position: absolute; left: 60%; top: 25%; width: 15%; height: 15%;text-align: center;">5</div>' +
				'<div style="font-size: 32px;position: absolute; left: 0%; top: 40%; width: 15%; height: 15%;text-align: center;">6</div>' +
				'<div style="font-size: 32px;position: absolute; left: 15%; top: 40%; width: 15%; height: 15%;text-align: center;">7</div>' +
				'<div style="font-size: 32px;position: absolute; left: 30%; top: 40%; width: 15%; height: 15%;text-align: center;">8</div>' +
				'<div style="font-size: 32px;position: absolute; left: 45%; top: 40%; width: 15%; height: 15%;text-align: center;">9</div>' +
				'<div style="font-size: 32px;position: absolute; left: 60%; top: 40%; width: 15%; height: 15%;text-align: center;">10</div>' +
				'</div>' +
				
				'<div style="position:absolute;left: 47%;top: 40%;color:#fff;">结束点</div>'+
				'<div id ="overdian" style="display:block; width: 70%; height: 70%; position: absolute; left: 24%; top: 45%; background: transparent;">' +
				'<div style="font-size: 32px;position: relative; left: 0%; top: 0%; width: 15%; height: 15%;text-align: center;">1</div>' +
				'<div style="font-size: 32px;position: relative; left: 15%; top: -15%; width: 15%; height: 15%;text-align: center;">2</div>' +
				'<div style="font-size: 32px;position: relative; left: 30%; top: -30%; width: 15%; height: 15%;text-align: center;">3</div>' +
				'<div style="font-size: 32px;position: relative; left: 45%; top: -45%; width: 15%; height: 15%;text-align: center;">4</div>' +
				'<div style="font-size: 32px;position: relative; left: 60%; top: -60%; width: 15%; height: 15%;text-align: center;">5</div>' +
				'<div style="font-size: 32px;position: relative; left: 0%; top: -60%; width: 15%; height: 15%;text-align: center;">6</div>' +
				'<div style="font-size: 32px;position: relative; left: 15%; top: -75%; width: 15%; height: 15%;text-align: center;">7</div>' +
				'<div style="font-size: 32px;position: relative; left: 30%; top: -90%; width: 15%; height: 15%;text-align: center;">8</div>' +
				'<div style="font-size: 32px;position: relative; left: 45%; top: -105%; width: 15%; height: 15%;text-align: center;">9</div>' +
				'<div style="font-size: 32px;position: relative; left: 60%; top: -120%; width: 15%; height: 15%;text-align: center;">10</div>' +
				'</div>' +
				
				'<div style="position:absolute;left: 48%;top: 70%;color:#fff;">速度<span id="speed2" style="color:#00FFFF;">100</span><span style="color:#00FFFF;">%</span></div>'+
				'<input id="move_speed" type="range" style="position:absolute;left: 22%;top: 77%;"/>'+
				
				'<div id="cancelsruebut"><span id="cancelbut">取消</span><span id="sruebut">确定</span></div>'
				
			);
			dialog.setHasTitleCloseButton(false);
			dialog.setDraggable(false);
			dialog.setVisible(true);
			dialog.setDisposeOnHide(true);
			//保存当前的点位和速度的值
			var a = block.getFieldValue('c1');
			var b = block.getFieldValue('c2');
			var c = block.getFieldValue('c3');
			//设置过渡点位
			var numdian = document.getElementById('numdian').getElementsByTagName('div');
			var dian = document.getElementById('dian');
			dian.innerHTML = block.getFieldValue('c1');
			$('#numdian div').eq(dian.innerHTML-1).css('color','#00C5AC');
			for(var i = 0; i < numdian.length; i++) {
				numdian[i].index = i;
				numdian[i].onclick=function() {
					block.setFieldValue(numdian_string[this.index], 'c1');
					//dian.innerHTML = block.getFieldValue('c1');
					$('#numdian div').eq(this.index).css('color','#00C5AC').siblings().css('color','#fff');
				}
			}
			//设置结束点位
			var overdian = document.getElementById('overdian').getElementsByTagName('div');
			var over = document.getElementById('over');
			over.innerHTML = block.getFieldValue('c2');
			$('#overdian div').eq(over.innerHTML-1).css('color','#00C5AC');
			for(var i = 0; i < overdian.length; i++) {
				overdian[i].index = i;
				overdian[i].onclick=function() {
					block.setFieldValue(numdian_string[this.index], 'c2');
					over.innerHTML = block.getFieldValue('c2');
					$('#overdian div').eq(this.index).css('color','#00C5AC').siblings().css('color','#fff');
				}
			}
			//设置速度
			var con;
			con=block.getFieldValue('c3');
			$("#move_speed").css("background-size",con+"%"+" "+100+"%");
			$("#speed").html(con);
			$("#speed2").html(con);
			$('#move_speed')[0].value=con;
			$('#move_speed').on('input propertychange',function(){
			 	con=$('#move_speed').val();
		        $("#move_speed").css("background-size",con+"%"+" "+100+"%");
		        $("#speed").html(con);
		        $("#speed2").html(con);
		        block.setFieldValue(con, 'c3');
		    });
			//取消设定的值，还原到原来的值
			$("#cancelbut").on('click',function(){
				block.setFieldValue(a, 'c1');
				block.setFieldValue(b, 'c2');
				block.setFieldValue(c, 'c3');
				$(".modal-dialog-bg").remove();
				$(".modal-dialog").remove();
			});
			//确定
			$("#sruebut").on('click',function(){
				$(".modal-dialog-bg").remove();
				$(".modal-dialog").remove();
			});

		}
	}
};
Blockly.JavaScript['tcr_c'] = function(block) {
	var b = block.getFieldValue('c1');
	var c = block.getFieldValue('c2');
	var d = block.getFieldValue('c3');
	var code = "C P[" + b + "]; P[" + c + "] " + d + "% FINE;";
	return code;
};
//标签
Blockly.Blocks['tcr_lbl'] = {
	init: function() {
		this.appendDummyInput()
			.appendField("标签")
			.appendField(new TCR.FieldTextInput("1"), "lbl1");
		this.setPreviousStatement(true, null);
		this.setNextStatement(true, null);
		this.setColour(90);
		this.setTooltip('');
		this.setHelpUrl('');
	},
	onchange: function(ev) {
		if(ev.type == Blockly.Events.UI && ev.element == 'click' && this.id == ev.blockId) {
			var block = this;
			var dialog = new goog.ui.Dialog();
			dialog.setContent(
				'<div id="futitle" style="position: absolute;top: 7%;left:0%;display: none;">标签点<span id="dian" style="color:#00FFFF;">1</span></div>'+
				
				'<div style="position:absolute;left: 47%;top: 26%;color:#fff;">标签</div>'+
				'<div id ="numdian" style="display:block; width: 70%; height: 70%; position: absolute; left: 24%; top: 14%; background: transparent;">' +
				'<div style="font-size: 32px;position: absolute; left: 0%; top: 25%; width: 15%; height: 15%;text-align: center;">1</div>' +
				'<div style="font-size: 32px;position: absolute; left: 15%; top: 25%; width: 15%; height: 15%;text-align: center;">2</div>' +
				'<div style="font-size: 32px;position: absolute; left: 30%; top: 25%; width: 15%; height: 15%;text-align: center;">3</div>' +
				'<div style="font-size: 32px;position: absolute; left: 45%; top: 25%; width: 15%; height: 15%;text-align: center;">4</div>' +
				'<div style="font-size: 32px;position: absolute; left: 60%; top: 25%; width: 15%; height: 15%;text-align: center;">5</div>' +
				'<div style="font-size: 32px;position: absolute; left: 0%; top: 40%; width: 15%; height: 15%;text-align: center;">6</div>' +
				'<div style="font-size: 32px;position: absolute; left: 15%; top: 40%; width: 15%; height: 15%;text-align: center;">7</div>' +
				'<div style="font-size: 32px;position: absolute; left: 30%; top: 40%; width: 15%; height: 15%;text-align: center;">8</div>' +
				'<div style="font-size: 32px;position: absolute; left: 45%; top: 40%; width: 15%; height: 15%;text-align: center;">9</div>' +
				'<div style="font-size: 32px;position: absolute; left: 60%; top: 40%; width: 15%; height: 15%;text-align: center;">10</div>' +
				'</div>' +
				
				'<div id="cancelsruebut"><span id="cancelbut">取消</span><span id="sruebut">确定</span></div>'
			);
			dialog.setHasTitleCloseButton(false);
			dialog.setDraggable(false);
			dialog.setVisible(true);
			dialog.setDisposeOnHide(true);
			//保存当前的点位的值
			var a = block.getFieldValue('lbl1');
			//设置点位
			var numdian = document.getElementById('numdian').getElementsByTagName('div');
			var dian = document.getElementById('dian');
			dian.innerHTML = block.getFieldValue('lbl1');
			$('#numdian div').eq(dian.innerHTML-1).css('color','#00C5AC');
			for(var i = 0; i < numdian.length; i++) {
				numdian[i].index = i;
				numdian[i].onclick=function() {
					block.setFieldValue(numdian_string[this.index], 'lbl1');
					dian.innerHTML = block.getFieldValue('lbl1');
					$('#numdian div').eq(this.index).css('color','#00C5AC').siblings().css('color','#fff');
				}
			}
			
			//取消设定的值，还原到原来的值
			$("#cancelbut").on('click',function(){
				block.setFieldValue(a, 'lbl1');
				$(".modal-dialog-bg").remove();
				$(".modal-dialog").remove();
			});
			//确定
			$("#sruebut").on('click',function(){
				$(".modal-dialog-bg").remove();
				$(".modal-dialog").remove();
			});

		}
	}
};
Blockly.JavaScript['tcr_lbl'] = function(block) {
	var b = block.getFieldValue('lbl1');
	var code = "LBL[" + b + "];";
	return code;
};
//跳转
Blockly.Blocks['tcr_jmp'] = {
	init: function() {
		this.appendDummyInput()
			.appendField("跳转到标签")
			.appendField(new TCR.FieldTextInput("1"), "jpm1");
		this.setPreviousStatement(true, null);
		this.setNextStatement(true, null);
		this.setColour(90);
		this.setTooltip('');
		this.setHelpUrl('');
	},
	onchange: function(ev) {
		if(ev.type == Blockly.Events.UI && ev.element == 'click' && this.id == ev.blockId) {
			var block = this;
			var dialog = new goog.ui.Dialog();
			dialog.setContent(
				'<div id="futitle" style="position: absolute;top: 7%;left:0%;display: none;">标签点<span id="dian" style="color:#00FFFF;">1</span></div>'+
				
				'<div style="position:absolute;left: 47%;top: 26%;color:#fff;">跳转到标签</div>'+
				'<div id ="numdian" style="display:block; width: 70%; height: 70%; position: absolute; left: 24%; top: 14%; background: transparent;">' +
				'<div style="font-size: 32px;position: absolute; left: 0%; top: 25%; width: 15%; height: 15%;text-align: center;">1</div>' +
				'<div style="font-size: 32px;position: absolute; left: 15%; top: 25%; width: 15%; height: 15%;text-align: center;">2</div>' +
				'<div style="font-size: 32px;position: absolute; left: 30%; top: 25%; width: 15%; height: 15%;text-align: center;">3</div>' +
				'<div style="font-size: 32px;position: absolute; left: 45%; top: 25%; width: 15%; height: 15%;text-align: center;">4</div>' +
				'<div style="font-size: 32px;position: absolute; left: 60%; top: 25%; width: 15%; height: 15%;text-align: center;">5</div>' +
				'<div style="font-size: 32px;position: absolute; left: 0%; top: 40%; width: 15%; height: 15%;text-align: center;">6</div>' +
				'<div style="font-size: 32px;position: absolute; left: 15%; top: 40%; width: 15%; height: 15%;text-align: center;">7</div>' +
				'<div style="font-size: 32px;position: absolute; left: 30%; top: 40%; width: 15%; height: 15%;text-align: center;">8</div>' +
				'<div style="font-size: 32px;position: absolute; left: 45%; top: 40%; width: 15%; height: 15%;text-align: center;">9</div>' +
				'<div style="font-size: 32px;position: absolute; left: 60%; top: 40%; width: 15%; height: 15%;text-align: center;">10</div>' +
				'</div>' +
				
				'<div id="cancelsruebut"><span id="cancelbut">取消</span><span id="sruebut">确定</span></div>'
			);
			dialog.setHasTitleCloseButton(false);
			dialog.setDraggable(false);
			dialog.setVisible(true);
			dialog.setDisposeOnHide(true);
			//保存当前的点位的值
			var a = block.getFieldValue('jpm1');
			//设置标签
			var numdian = document.getElementById('numdian').getElementsByTagName('div');
			var dian = document.getElementById('dian');
			dian.innerHTML = block.getFieldValue('jpm1');
			$('#numdian div').eq(dian.innerHTML-1).css('color','#00C5AC');
			for(var i = 0; i < numdian.length; i++) {
				numdian[i].index = i;
				numdian[i].onclick=function() {
					block.setFieldValue(numdian_string[this.index], 'jpm1');
					dian.innerHTML = block.getFieldValue('jpm1');
					$('#numdian div').eq(this.index).css('color','#00C5AC').siblings().css('color','#fff');
				}
			}
			
			//取消设定的值，还原到原来的值
			$("#cancelbut").on('click',function(){
				block.setFieldValue(a, 'jpm1');
				$(".modal-dialog-bg").remove();
				$(".modal-dialog").remove();
			});
			//确定
			$("#sruebut").on('click',function(){
				$(".modal-dialog-bg").remove();
				$(".modal-dialog").remove();
			});

		}
	}
};
Blockly.JavaScript['tcr_jmp'] = function(block) {
	var b = block.getFieldValue('jpm1');
	var code = "JMP LBL[" + b + "];";
	return code;
};
//等待
Blockly.Blocks['tcr_wait'] = {
	init: function() {
		this.appendDummyInput()
			.appendField("等待")
			.appendField(new TCR.FieldTextInput("1"), "wait1")
			.appendField("秒");
		this.setPreviousStatement(true, null);
		this.setNextStatement(true, null);
		this.setColour(90);
		this.setTooltip('');
		this.setHelpUrl('');
	},
	onchange: function(ev) {
		if(ev.type == Blockly.Events.UI && ev.element == 'click' && this.id == ev.blockId) {
			var block = this;
			var dialog = new goog.ui.Dialog();
			dialog.setContent(
				'<div id="futitle" style="position: absolute;top: 7%;left:0%;display: none;">等待秒数<span id="dian2" style="color:#00FFFF;">1</span></div>'+
				
				'<div id="dian" style="position:absolute;left: 24%;top: 13%;color:#00FFFF;font-size: 50px;width: 50%;text-align: center;">1</div>'+
				'<div id ="numdian" style="display:block; width: 70%; height: 70%; position: absolute; left: 24%; top: 14%; background: transparent;">' +
				'<div style="font-size: 32px;position: absolute; left: 13%; top: 20%; width: 15%; height: 15%;text-align: center;">1</div>' +
				'<div style="font-size: 32px;position: absolute; left: 30%; top: 20%; width: 15%; height: 15%;text-align: center;">2</div>' +
				'<div style="font-size: 32px;position: absolute; left: 47%; top: 20%; width: 15%; height: 15%;text-align: center;">3</div>' +
				'<div style="font-size: 32px;position: absolute; left: 13%; top: 40%; width: 15%; height: 15%;text-align: center;">4</div>' +
				'<div style="font-size: 32px;position: absolute; left: 30%; top: 40%; width: 15%; height: 15%;text-align: center;">5</div>' +
				'<div style="font-size: 32px;position: absolute; left: 47%; top: 40%; width: 15%; height: 15%;text-align: center;">6</div>' +
				'<div style="font-size: 32px;position: absolute; left: 13%; top: 60%; width: 15%; height: 15%;text-align: center;">7</div>' +
				'<div style="font-size: 32px;position: absolute; left: 30%; top: 60%; width: 15%; height: 15%;text-align: center;">8</div>' +
				'<div style="font-size: 32px;position: absolute; left: 47%; top: 60%; width: 15%; height: 15%;text-align: center;">9</div>' +
				'<div style="font-size: 32px;position: absolute; left: 13%; top: 80%; width: 15%; height: 15%;text-align: center;">0</div>' +
				'<div style="font-size: 32px;position: absolute; left: 30%; top: 80%; width: 15%; height: 15%;text-align: center;">.</div>' +
				'</div>' +
				'<div style="font-size: 22px;position: absolute; left:58%; top: 71%; width: 9%; text-align: center;color:#fff;" id="cancellast">删除</div>' +
				'<div id="cancelsruebut"><span id="cancelbut">取消</span><span id="sruebut">确定</span></div>'
			);
			dialog.setHasTitleCloseButton(false);
			dialog.setDraggable(false);
			dialog.setVisible(true);
			dialog.setDisposeOnHide(true);
			//保存当前的点位的值
			var b = block.getFieldValue('wait1');
			//设置点位
			var numdian = document.getElementById('numdian').getElementsByTagName('div');
			var dian = document.getElementById('dian');
			dian.innerHTML = block.getFieldValue('wait1');
			block.setFieldValue("", 'wait1');
			for(var i = 0; i < numdian.length; i++) {
				numdian[i].index = i;
				numdian[i].addEventListener('click', function() {
					var a = block.getFieldValue('wait1');
					dian.innerHTML = block.getFieldValue('wait1');
					if(a.length<5){
						block.setFieldValue(a + numdian_sec_string[this.index], 'wait1');
					}
					if(a.length<6&&dian.innerHTML>=10){
						block.setFieldValue(a + numdian_sec_string[this.index], 'wait1');
					}
					dian.innerHTML = block.getFieldValue('wait1');
					$('#numdian div').eq(this.index).css('color','#00C5AC').siblings().css('color','#fff');
				});
			}
			$("#cancellast").on('click',function(){
				block.setFieldValue('', 'wait1');
				dian.innerHTML = '';
			});
			//取消设定的值，还原到原来的值
			$("#cancelbut").on('click',function(){
				block.setFieldValue(b, 'wait1');
				$(".modal-dialog-bg").remove();
				$(".modal-dialog").remove();
			});
			//确定
			$("#sruebut").on('click',function(){
				$(".modal-dialog-bg").remove();
				$(".modal-dialog").remove();
			});

		}
	}
};
Blockly.JavaScript['tcr_wait'] = function(block) {
	var b = block.getFieldValue('wait1');
	var code = "WAIT " + b + "sec;";
	return code;
};
//如果 寄存器
Blockly.Blocks['tcr_if_r'] = {
	init: function() {
		this.appendDummyInput()
			.appendField("如果变量")
			.appendField(new TCR.FieldTextInput("1"), "if1")
			.appendField(new TCR.FieldTextInput("="), "if2")
			.appendField(new TCR.FieldTextInput("1"), "if3")
			.appendField("跳转到标签")
			.appendField(new TCR.FieldTextInput("1"), "if4");
		this.setInputsInline(true);
		this.setPreviousStatement(true, null);
		this.setNextStatement(true, null);
		this.setColour(90);
		this.setTooltip('');
		this.setHelpUrl('');
	},
	onchange: function(ev) {
		if(ev.type == Blockly.Events.UI && ev.element == 'click' && this.id == ev.blockId) {
			var block = this;
			var dialog = new goog.ui.Dialog();
			dialog.setContent(
				'<div id="futitle" style="position: absolute;top: 12%;left:0%;display: none;">变量</div>'+
				
				'<div style="position:absolute;left: 45%;top: 5%;color:#fff;">变量一<span id="var1" style="color:#00FFFF;">0</span></div>'+
				'<input id="var1_val" type="range" min="1" style="position:absolute;left: 22%;top: 13%;z-index:1000;"/>'+
				
				'<div style="position:absolute;left: 45%;top: 18%;color:#fff;">逻辑符号 <span id="suan" style="color:#00FFFF;">1</span></div>'+
				'<div id ="fuhaodian" style="display:block; width: 70%; height: 70%; position: absolute; left: 24%; top: 8%; background: transparent;">' +
				'<div style="font-size: 32px;position: absolute; left: 8%; top: 20%; width: 15%; height: 15%;text-align: center;">=</div>' +
				'<div style="font-size: 32px;position: absolute; left: 24%; top: 20%; width: 15%; height: 15%;text-align: center;">≠</div>' +
				'<div style="font-size: 32px;position: absolute; left: 40%; top: 20%; width: 15%; height: 15%;text-align: center;">></div>' +
				'<div style="font-size: 32px;position: absolute; left: 56%; top: 20%; width: 15%; height: 15%;text-align: center;"><</div>' +
				'</div>' +
				
				'<div style="position:absolute;left: 45%;top: 35%;color:#fff;">变量二<span id="var2" style="color:#00FFFF;">1</span></div>'+
				'<input id="var2_val" type="range" min="1" style="position:absolute;left: 22%;top: 43%;z-index:1000;"/>'+
				
				'<div style="position:absolute;left: 45%;top: 49%;color:#fff;">跳转到标签<span id="dian" style="color:#00FFFF;">1</span></div>'+
				'<div id ="numdian" style="display:block; width: 70%; height: 70%; position: absolute; left: 24%; top: 37%; background: transparent;">' +
				'<div style="font-size: 32px;position: absolute; left: 0%; top: 25%; width: 15%; height: 15%;text-align: center;">1</div>' +
				'<div style="font-size: 32px;position: absolute; left: 15%; top: 25%; width: 15%; height: 15%;text-align: center;">2</div>' +
				'<div style="font-size: 32px;position: absolute; left: 30%; top: 25%; width: 15%; height: 15%;text-align: center;">3</div>' +
				'<div style="font-size: 32px;position: absolute; left: 45%; top: 25%; width: 15%; height: 15%;text-align: center;">4</div>' +
				'<div style="font-size: 32px;position: absolute; left: 60%; top: 25%; width: 15%; height: 15%;text-align: center;">5</div>' +
				'<div style="font-size: 32px;position: absolute; left: 0%; top: 40%; width: 15%; height: 15%;text-align: center;">6</div>' +
				'<div style="font-size: 32px;position: absolute; left: 15%; top: 40%; width: 15%; height: 15%;text-align: center;">7</div>' +
				'<div style="font-size: 32px;position: absolute; left: 30%; top: 40%; width: 15%; height: 15%;text-align: center;">8</div>' +
				'<div style="font-size: 32px;position: absolute; left: 45%; top: 40%; width: 15%; height: 15%;text-align: center;">9</div>' +
				'<div style="font-size: 32px;position: absolute; left: 60%; top: 40%; width: 15%; height: 15%;text-align: center;">10</div>' +
				'</div>' +
				
				'<div id="cancelsruebut"><span id="cancelbut">取消</span><span id="sruebut">确定</span></div>'
			);
			dialog.setHasTitleCloseButton(false);
			dialog.setDraggable(false);
			dialog.setVisible(true);
			dialog.setDisposeOnHide(true);
			//保存当前的点位和速度的值
			var a = block.getFieldValue('if1');
			var b = block.getFieldValue('if2');
			var c = block.getFieldValue('if3');
			var d = block.getFieldValue('if4');
			//设置变量1
			var can;
			can=block.getFieldValue('if1');
			$("#var1_val").css("background-size",can+"%"+" "+100+"%");
			$("#var1").html(can);
			$('#var1_val')[0].value=can;
			$('#var1_val').on('input propertychange',function(){
			 	can=$('#var1_val').val();
		        $("#var1_val").css("background-size",can+"%"+" "+100+"%");
		        $("#var1").html(can);
		        block.setFieldValue(can, 'if1');
		    });
			//设置运算符号
			 var fuhaodian = document.getElementById('fuhaodian').getElementsByTagName('div');
			var suan = document.getElementById('suan');
			suan.innerHTML = block.getFieldValue('if2');
			if(b==='='){
				$('#fuhaodian div').eq(0).css('color','#00C5AC').siblings().css('color','#fff');
			}
			if(b==='≠'){
				$('#fuhaodian div').eq(1).css('color','#00C5AC').siblings().css('color','#fff');
			}
			if(b==='>'){
				$('#fuhaodian div').eq(2).css('color','#00C5AC').siblings().css('color','#fff');
			}
			if(b==='<'){
				$('#fuhaodian div').eq(3).css('color','#00C5AC').siblings().css('color','#fff');
			}
			for(var i = 0; i < fuhaodian.length; i++) {
				fuhaodian[i].index = i;
				fuhaodian[i].onclick=function() {
					block.setFieldValue(fuhaodian_string[this.index], 'if2');
					suan.innerHTML = block.getFieldValue('if2');
					$('#fuhaodian div').eq(this.index).css('color','#00C5AC').siblings().css('color','#fff');
				}
			}
		    //设置变量2
			var ccn;
			ccn=block.getFieldValue('if3');
			$("#var2_val").css("background-size",ccn+"%"+" "+100+"%");
			$("#var2").html(ccn);
			$('#var2_val')[0].value=ccn;
			$('#var2_val').on('input propertychange',function(){
			 	ccn=$('#var2_val').val();
		        $("#var2_val").css("background-size",ccn+"%"+" "+100+"%");
		        $("#var2").html(ccn);
		        block.setFieldValue(ccn, 'if3');
		    });
		    //设置跳转标签
		    var numdian = document.getElementById('numdian').getElementsByTagName('div');
			var dian = document.getElementById('dian');
			dian.innerHTML = block.getFieldValue('if4');
			$('#numdian div').eq(dian.innerHTML-1).css('color','#00C5AC');
			for(var i = 0; i < numdian.length; i++) {
				numdian[i].index = i;
				numdian[i].onclick=function() {
					block.setFieldValue(numdian_string[this.index], 'if4');
					dian.innerHTML = block.getFieldValue('if4');
					$('#numdian div').eq(this.index).css('color','#00C5AC').siblings().css('color','#fff');
				}
			}
			//取消设定的值，还原到原来的值
			$("#cancelbut").on('click',function(){
				block.setFieldValue(a, 'if1');
				block.setFieldValue(b, 'if2');
				block.setFieldValue(c, 'if3');
				block.setFieldValue(d, 'if4');
				$(".modal-dialog-bg").remove();
				$(".modal-dialog").remove();
			});
			//确定
			$("#sruebut").on('click',function(){
				$(".modal-dialog-bg").remove();
				$(".modal-dialog").remove();
			});
		}
	}
};
Blockly.JavaScript['tcr_if_r'] = function(block) {
	var b = block.getFieldValue('if1');
	var c = block.getFieldValue('if2');
	var d = block.getFieldValue('if3');
	var e = block.getFieldValue('if4');
	if(c=='≠'){
		c='<>';
	}
	var code = "IF R[" + b + "]" + c + d + ", JMP LBL[" + e + "];";
	return code;
};
//如果 端口
Blockly.Blocks['tcr_if_dido'] = {
	init: function() {
		this.appendDummyInput()
			.appendField("如果端口")
			.appendField(new TCR.FieldTextInput("DI"), "if1")
			.appendField(new TCR.FieldTextInput("1"), "if2")
			.appendField("=")
			.appendField(new TCR.FieldTextInput("ON"), "if3")
			.appendField("跳转到标签")
			.appendField(new TCR.FieldTextInput("1"), "if4");
		this.setInputsInline(true);
		this.setPreviousStatement(true, null);
		this.setNextStatement(true, null);
		this.setColour(90);
		this.setTooltip('');
		this.setHelpUrl('');
	},
	onchange: function(ev) {
		if(ev.type == Blockly.Events.UI && ev.element == 'click' && this.id == ev.blockId) {
			var block = this;
			var dialog = new goog.ui.Dialog();
			dialog.setContent(
				'<div id="futitle" style="position: absolute;top: 12%;left:0%;display: none;">端口</div>'+
				
				'<div style="position:absolute;left: 45%;top: 9%;color:#fff;display: none;">端口 <span id="dido" style="color:#00FFFF;">DI</span></div>'+
				'<div id ="didodian" style="display:block; width: 70%; height: 70%; position: absolute; left: 29%; top: -4%; background: transparent;">' +
				'<div style="font-size: 32px;position: absolute; left: 12%; top: 26%; width: 15%; height: 15%;text-align: center;color:#fff;z-index:999;">DI</div>' +
				'<div style="font-size: 32px;position: absolute; left: 27%; top: 26%; width: 15%; height: 15%;text-align: center;color:#fff;z-index:999;">DO</div>' +
				'</div>' +
					
				'<div style="position:absolute;left: 45%;top: 23%;color:#fff;">端口<span id="var1" style="color:#00FFFF;">1</span></div>'+
				'<input id="var1_val" type="range" min="1" max="16" style="position:absolute;left: 22%;top: 31%;z-index:1000;"/>'+
				
				'<div style="position:absolute;left: 45%;top: 36%;color:#fff;display: none;">开关<span id="swit" style="color:#00FFFF;">ON</span></div>'+
				'<div id ="swtichdian" style="display:block; width: 70%; height: 70%; position: absolute; left: 29%; top: 28%; background: transparent;">' +
				'<div style="font-size: 32px;position: absolute; left: 12%; top: 18%; width: 15%; height: 15%;text-align: center;z-index:999;">ON</div>' +
				'<div style="font-size: 32px;position: absolute; left: 30%; top: 18%; width: 15%; height: 15%;text-align: center;z-index:999;">OFF</div>' +
				'</div>' +
				
				'<div style="position:absolute;left: 45%;top: 49%;color:#fff;display: none;">跳转到标签<span id="dian" style="color:#00FFFF;">1</span></div>'+
				'<div id ="numdian" style="display:block; width: 70%; height: 70%; position: absolute; left: 24%; top: 37%; background: transparent;">' +
				'<div style="font-size: 32px;position: absolute; left: 0%; top: 25%; width: 15%; height: 15%;text-align: center;">1</div>' +
				'<div style="font-size: 32px;position: absolute; left: 15%; top: 25%; width: 15%; height: 15%;text-align: center;">2</div>' +
				'<div style="font-size: 32px;position: absolute; left: 30%; top: 25%; width: 15%; height: 15%;text-align: center;">3</div>' +
				'<div style="font-size: 32px;position: absolute; left: 45%; top: 25%; width: 15%; height: 15%;text-align: center;">4</div>' +
				'<div style="font-size: 32px;position: absolute; left: 60%; top: 25%; width: 15%; height: 15%;text-align: center;">5</div>' +
				'<div style="font-size: 32px;position: absolute; left: 0%; top: 40%; width: 15%; height: 15%;text-align: center;">6</div>' +
				'<div style="font-size: 32px;position: absolute; left: 15%; top: 40%; width: 15%; height: 15%;text-align: center;">7</div>' +
				'<div style="font-size: 32px;position: absolute; left: 30%; top: 40%; width: 15%; height: 15%;text-align: center;">8</div>' +
				'<div style="font-size: 32px;position: absolute; left: 45%; top: 40%; width: 15%; height: 15%;text-align: center;">9</div>' +
				'<div style="font-size: 32px;position: absolute; left: 60%; top: 40%; width: 15%; height: 15%;text-align: center;">10</div>' +
				'</div>' +
				
				'<div id="cancelsruebut"><span id="cancelbut">取消</span><span id="sruebut">确定</span></div>'
			);
			dialog.setHasTitleCloseButton(false);
			dialog.setDraggable(false);
			dialog.setVisible(true);
			dialog.setDisposeOnHide(true);
			//保存当前的点位和速度的值
			var a = block.getFieldValue('if1');
			var b = block.getFieldValue('if2');
			var c = block.getFieldValue('if3');
			var d = block.getFieldValue('if4');
			//设置端口
			var didodian = document.getElementById('didodian').getElementsByTagName('div');
			var dido = document.getElementById('dido');
			dido.innerHTML = block.getFieldValue('if1');
			if(a==='DI'){
				$('#didodian div').eq(0).css('color','#00C5AC').siblings().css('color','#fff');
			}
			if(a==='DO'){
				$('#didodian div').eq(1).css('color','#00C5AC').siblings().css('color','#fff');
			}
			for(var i = 0; i < didodian.length; i++) {
				didodian[i].index = i;
				didodian[i].onclick=function() {
					block.setFieldValue(didodian_string[this.index], 'if1');
					dido.innerHTML = block.getFieldValue('if1');
					$('#didodian div').eq(this.index).css('color','#00C5AC').siblings().css('color','#fff');
				}
			}
			//设置变量
			var ccn;
			ccn=block.getFieldValue('if2');
			$("#var1_val").css("background-size",ccn*6.25+"%"+" "+100+"%");
			$("#var1").html(ccn);
			$('#var1_val')[0].value=ccn;
			$('#var1_val').on('input propertychange',function(){
			 	ccn=$('#var1_val').val();
		        $("#var1_val").css("background-size",ccn*6.25+"%"+" "+100+"%");
		        $("#var1").html(ccn);
		        block.setFieldValue(ccn, 'if2');
		    });
			//设置开关
			var swtichdian = document.getElementById('swtichdian').getElementsByTagName('div');
			var swit = document.getElementById('swit');
			swit.innerHTML = block.getFieldValue('if3');
			if(c==='ON'){
				$('#swtichdian div').eq(0).css('color','#00C5AC').siblings().css('color','#fff');
			}
			if(c==='OFF'){
				$('#swtichdian div').eq(1).css('color','#00C5AC').siblings().css('color','#fff');
			}
			for(var i = 0; i < swtichdian.length; i++) {
				swtichdian[i].index = i;
				swtichdian[i].onclick=function() {
					block.setFieldValue(swtichdian_string[this.index], 'if3');
					swit.innerHTML = block.getFieldValue('if3');
					$('#swtichdian div').eq(this.index).css('color','#00C5AC').siblings().css('color','#fff');
				}
			}
		    //设置跳转标签
		    var numdian = document.getElementById('numdian').getElementsByTagName('div');
			var dian = document.getElementById('dian');
			dian.innerHTML = block.getFieldValue('if4');
			$('#numdian div').eq(dian.innerHTML-1).css('color','#00C5AC');
			for(var i = 0; i < numdian.length; i++) {
				numdian[i].index = i;
				numdian[i].onclick=function() {
					block.setFieldValue(numdian_string[this.index], 'if4');
					dian.innerHTML = block.getFieldValue('if4');
					$('#numdian div').eq(this.index).css('color','#00C5AC').siblings().css('color','#fff');
				}
			}
			//取消设定的值，还原到原来的值
			$("#cancelbut").on('click',function(){
				block.setFieldValue(a, 'if1');
				block.setFieldValue(b, 'if2');
				block.setFieldValue(c, 'if3');
				block.setFieldValue(d, 'if4');
				$(".modal-dialog-bg").remove();
				$(".modal-dialog").remove();
			});
			//确定
			$("#sruebut").on('click',function(){
				$(".modal-dialog-bg").remove();
				$(".modal-dialog").remove();
			});
		}
	}
};
Blockly.JavaScript['tcr_if_dido'] = function(block) {
	var b = block.getFieldValue('if1');
	var c = block.getFieldValue('if2');
	var d = block.getFieldValue('if3');
	var e = block.getFieldValue('if4');
	var code = "IF " + b + "[" + c + "]=" + d + ", JMP LBL[" + e + "];";
	return code;
};

//设置变量1
Blockly.Blocks['tcr_r1'] = {
	init: function() {
		this.appendDummyInput()
			.appendField("设置变量")
			.appendField(new TCR.FieldTextInput("1"), "r1")
			.appendField("=")
			.appendField(new TCR.FieldTextInput("常数"), "r2")
			.appendField(new TCR.FieldTextInput("1"), "r3");
		this.setPreviousStatement(true, null);
		this.setNextStatement(true, null);
		this.setColour(180);
		this.setTooltip('');
		this.setHelpUrl('');
	},
	onchange: function(ev) {
		if(ev.type == Blockly.Events.UI && ev.element == 'click' && this.id == ev.blockId) {
			var block = this;
			var dialog = new goog.ui.Dialog();
			dialog.setContent(
				'<div id="futitle" style="position: absolute;top: 12%;left:0%;display: none;">变量</div>'+
					
				'<div style="position:absolute;left: 45%;top: 20%;color:#fff;">变量<span id="var1" style="color:#00FFFF;">1</span></div>'+
				'<input id="var1_val" type="range" min="1" style="position:absolute;left: 22%;top: 28%;z-index:1000;"/>'+
				
				'<div style="position:absolute;left: 45%;top: 49%;color:#fff;display: none;"><span id="dian" style="color:#00FFFF;">1</span></div>'+
				'<div id ="numdian" style="display:block; width: 70%; height: 70%; position: absolute; left: 24%; top: 10%; background: transparent;">' +
				'<div style="font-size: 32px;position: absolute; left: 15%; top: 40%; width: 15%; height: 15%;text-align: center;">常数</div>' +
				'<div style="font-size: 32px;position: absolute; left: 40%; top: 40%; width: 15%; height: 15%;text-align: center;">变量</div>' +
				'</div>' +
				
				'<div style="position:absolute;left: 45%;top: 50%;color:#fff;"><span id="var3">常数</span><span id="var2" style="color:#00FFFF;">1</span></div>'+
				'<input id="var2_val" type="range" min="1" style="position:absolute;left: 22%;top: 58%;z-index:1000;"/>'+
				
				'<div id="cancelsruebut"><span id="cancelbut">取消</span><span id="sruebut">确定</span></div>'
			);
			dialog.setHasTitleCloseButton(false);
			dialog.setDraggable(false);
			dialog.setVisible(true);
			dialog.setDisposeOnHide(true);
			//保存当前的点位和速度的值
			var a = block.getFieldValue('r1');
			var b = block.getFieldValue('r2');
			var c = block.getFieldValue('r3');
			
			//设置变量
			var can;
			can=block.getFieldValue('r1');
			$("#var1_val").css("background-size",can+"%"+" "+100+"%");
			$("#var1").html(can);
			$('#var1_val')[0].value=can;
			$('#var1_val').on('input propertychange',function(){
			 	can=$('#var1_val').val();
		        $("#var1_val").css("background-size",can+"%"+" "+100+"%");
		        $("#var1").html(can);
		        block.setFieldValue(can, 'r1');
		    });
			//更改常数与变量
			var numdian = document.getElementById('numdian').getElementsByTagName('div');
			$("#dian").html(block.getFieldValue('r2'));
			$("#var3").html(block.getFieldValue('r2'));
			if(b==='常数'){
				$('#numdian div').eq(0).css('color','#00C5AC').siblings().css('color','#fff');
			}
			if(b==='变量'){
				$('#numdian div').eq(1).css('color','#00C5AC').siblings().css('color','#fff');
			}
			for(var i = 0; i < numdian.length; i++) {
				numdian[i].index = i;
				numdian[i].onclick=function() {
					block.setFieldValue(numvar_string[this.index], 'r2');
					$("#dian").html(block.getFieldValue('r2'));
					$("#var3").html(numvar_string[this.index]);
					$('#numdian div').eq(this.index).css('color','#00C5AC').siblings().css('color','#fff');
				}
			}
			//
			var ccn;
			ccn=block.getFieldValue('r3');
			$("#var2_val").css("background-size",ccn+"%"+" "+100+"%");
			$("#var2").html(ccn);
			$('#var2_val')[0].value=ccn;
			$('#var2_val').on('input propertychange',function(){
			 	ccn=$('#var2_val').val();
		        $("#var2_val").css("background-size",ccn+"%"+" "+100+"%");
		        $("#var2").html(ccn);
		        block.setFieldValue(ccn, 'r3');
		    });
			//取消设定的值，还原到原来的值
			$("#cancelbut").on('click',function(){
				block.setFieldValue(a, 'r1');
				block.setFieldValue(b, 'r2');
				block.setFieldValue(c, 'r3');
				$(".modal-dialog-bg").remove();
				$(".modal-dialog").remove();
			});
			//确定
			$("#sruebut").on('click',function(){
				$(".modal-dialog-bg").remove();
				$(".modal-dialog").remove();
			});
		}
	}
};
Blockly.JavaScript['tcr_r1'] = function(block) {
	var b = block.getFieldValue('r1');
	var c = block.getFieldValue('r2');
	var d = block.getFieldValue('r3');
	var code;
	if(c=='常数'){
		c='';
		code = "R[" + b + "]=" + c + d + ";";
	}
	if(c=='变量'){
		c='R['+d+']';
		code = "R[" + b + "]=" + c + ";";
	}
	return code;
};
Blockly.Blocks['tcr_r2'] = {
	init: function() {
		this.appendDummyInput()
			.appendField("变量")
			.appendField(new TCR.FieldTextInput("1"), "rr");
		this.setOutput(true, null);
		this.setColour(180);
		this.setTooltip('');
		this.setHelpUrl('');
	},
	onchange: function(ev) {
		if(ev.type == Blockly.Events.UI && ev.element == 'click' && this.id == ev.blockId) {
			var block = this;
			var dialog = new goog.ui.Dialog();
			dialog.setContent(
				'<div id="futitle" style="position: absolute;top: 12%;left:0%;display: none;">变量</div>'+
					
				'<div style="position:absolute;left: 45%;top: 33%;color:#fff;">变量<span id="var1" style="color:#00FFFF;">1</span></div>'+
				'<input id="var1_val" type="range" min="1" style="position:absolute;left: 22%;top: 41%;z-index:1000;"/>'+
				
				'<div id="cancelsruebut"><span id="cancelbut">取消</span><span id="sruebut">确定</span></div>'
			);
			dialog.setHasTitleCloseButton(false);
			dialog.setDraggable(false);
			dialog.setVisible(true);
			dialog.setDisposeOnHide(true);
			//保存当前的点位和速度的值
			var a = block.getFieldValue('rr');
			
			//设置变量
			var ccn;
			ccn=block.getFieldValue('rr');
			$("#var1_val").css("background-size",ccn+"%"+" "+100+"%");
			$("#var1").html(ccn);
			$('#var1_val')[0].value=ccn;
			$('#var1_val').on('input propertychange',function(){
			 	ccn=$('#var1_val').val();
		        $("#var1_val").css("background-size",ccn+"%"+" "+100+"%");
		        $("#var1").html(ccn);
		        block.setFieldValue(ccn, 'rr');
		    });
		
			//取消设定的值，还原到原来的值
			$("#cancelbut").on('click',function(){
				block.setFieldValue(a, 'rr');
				$(".modal-dialog-bg").remove();
				$(".modal-dialog").remove();
			});
			//确定
			$("#sruebut").on('click',function(){
				$(".modal-dialog-bg").remove();
				$(".modal-dialog").remove();
			});
		}
	}
};
Blockly.JavaScript['tcr_r2'] = function(block) {
	var b = block.getFieldValue('rr');
	var code = "R[" + b + "]";
	return [code, Blockly.JavaScript.ORDER_ATOMIC];
};
//设置变量运算
Blockly.Blocks['tcr_r_opera'] = {
	init: function() {
		this.appendDummyInput()
			.appendField("设置变量")
			.appendField(new TCR.FieldTextInput("1"), "var1")
			.appendField("=")
			.appendField(new TCR.FieldTextInput("变量"), "var2")
			.appendField(new TCR.FieldTextInput("1"), "var3")
			.appendField(new TCR.FieldTextInput("+"), "var4")
			.appendField(new TCR.FieldTextInput("常数"), "var5")
			.appendField(new TCR.FieldTextInput("1"), "var6");
		this.setPreviousStatement(true, null);
		this.setNextStatement(true, null);
		this.setColour(180);
		this.setTooltip('');
		this.setHelpUrl('');
	},
	onchange: function(ev) {
		if(ev.type == Blockly.Events.UI && ev.element == 'click' && this.id == ev.blockId) {
			var block = this;
			var dialog = new goog.ui.Dialog();
			dialog.setContent(
				'<div id="futitle" style="position: absolute;top: 5%;left:0%;">设置变量<span id="var11" style="color:#00FFFF;">1</span>=<span id="dian" style="color:#00FFFF;">变量</span><span id="var3" style="color:#00FFFF;">1</span><span id="yun" style="color:#00FFFF;">+</span><span id="dian5" style="color:#00FFFF;">常数</span><span id="var6" style="color:#00FFFF;">1</span></div>'+
				
				'<div style="position:absolute;left: 45%;top: 13%;color:#fff;">变量<span id="var1" style="color:#00FFFF;">1</span></div>'+
				'<input id="var1_val" type="range" min="1" style="position:absolute;left: 22%;top: 20%;z-index:1000;"/>'+
				
				'<div id ="numdian" style="display:block; width: 50%; height: 20%; position: absolute; left: 27%; top: 19%;">' +
				'<div style="font-size: 26px;position: absolute; left: 15%; top: 40%; width: 30%; height: 40%;text-align: center;">常数</div>' +
				'<div style="font-size: 26px;position: absolute; left: 40%; top: 40%; width: 30%; height: 40%;text-align: center;">变量</div>' +
				'</div>' +
				
				'<input id="var3_val" type="range" min="1" style="position:absolute;left: 22%;top: 38%;z-index:1000;"/>'+
				
				'<div style="position:absolute;left: 45%;top: 45%;color:#fff;">运算符号</div>'+
				'<div id ="yundian" style="display:block; width: 60%; height: 30%; position: absolute; left: 26%; top: 45%;">' +
				'<div style="font-size: 32px;position: absolute; left: 8%; top: 18%; width: 15%; height: 26%;text-align: center;z-index:1000;">+</div>' +
				'<div style="font-size: 32px;position: absolute; left: 23%; top: 18%; width: 15%; height: 26%;text-align: center;z-index:1000;">-</div>' +
				'<div style="font-size: 32px;position: absolute; left: 38%; top: 18%; width: 15%; height: 26%;text-align: center;z-index:1000;">×</div>' +
				'<div style="font-size: 32px;position: absolute; left: 53%; top: 18%; width: 15%; height: 26%;text-align: center;z-index:1000;">÷</div>' +
				'</div>' +
				
				'<div id ="numdian5" style="display:block; width: 50%; height: 20%; position: absolute; left: 27%; top: 54%; color:#fff;">' +
				'<div style="font-size: 26px;position: absolute; left: 15%; top: 40%; width: 30%; height: 40%;text-align: center;">常数</div>' +
				'<div style="font-size: 26px;position: absolute; left: 40%; top: 40%; width: 30%; height: 40%;text-align: center;">变量</div>' +
				'</div>' +
				
				'<input id="var6_val" type="range" min="1" style="position:absolute;left: 22%;top: 74%;z-index:1000;"/>'+
				
				'<div id="cancelsruebut"><span id="cancelbut">取消</span><span id="sruebut">确定</span></div>'
			);
			dialog.setHasTitleCloseButton(false);
			dialog.setDraggable(false);
			dialog.setVisible(true);
			dialog.setDisposeOnHide(true);
			//保存当前的点位和速度的值
			var a = block.getFieldValue('var1');
			var b = block.getFieldValue('var2');
			var c = block.getFieldValue('var3');
			var d = block.getFieldValue('var4');
			var e = block.getFieldValue('var5');
			var f = block.getFieldValue('var6');
			
			//设置变量
			var can;
			can=block.getFieldValue('var1');
			$("#var1_val").css("background-size",can+"%"+" "+100+"%");
			$("#var1").html(can);
			$("#var11").html(can);
			$('#var1_val')[0].value=can;
			$('#var1_val').on('input propertychange',function(){
			 	can=$('#var1_val').val();
		        $("#var1_val").css("background-size",can+"%"+" "+100+"%");
		        $("#var1").html(can);
		        $("#var11").html(can);
		        block.setFieldValue(can, 'var1');
		    });
		    //更改常数与变量
			var numdian = document.getElementById('numdian').getElementsByTagName('div');
			$("#dian").html(block.getFieldValue('var2'));
			if(b==='常数'){
				$('#numdian div').eq(0).css('color','#00C5AC').siblings().css('color','#fff');
			}
			if(b==='变量'){
				$('#numdian div').eq(1).css('color','#00C5AC').siblings().css('color','#fff');
			}
			for(var i = 0; i < numdian.length; i++) {
				numdian[i].index = i;
				numdian[i].onclick=function() {
					block.setFieldValue(numvar_string[this.index], 'var2');
					$("#dian").html(block.getFieldValue('var2'));
					$('#numdian div').eq(this.index).css('color','#00C5AC').siblings().css('color','#fff');
				}
			}
			var ccn;
			ccn=block.getFieldValue('var3');
			$("#var3_val").css("background-size",ccn+"%"+" "+100+"%");
			$("#var3").html(ccn);
			$('#var3_val')[0].value=ccn;
			$('#var3_val').on('input propertychange',function(){
			 	ccn=$('#var3_val').val();
		        $("#var3_val").css("background-size",ccn+"%"+" "+100+"%");
		        $("#var3").html(ccn);
		        block.setFieldValue(ccn, 'var3');
		    });
			//设置运算符号
			var yundian = document.getElementById('yundian').getElementsByTagName('div');
			var yun = document.getElementById('yun');
			yun.innerHTML = block.getFieldValue('var4');
			if(d==='+'){
				$('#yundian div').eq(0).css('color','#00C5AC').siblings().css('color','#fff');
			}
			if(d==='-'){
				$('#yundian div').eq(1).css('color','#00C5AC').siblings().css('color','#fff');
			}
			if(d==='*'){
				$('#yundian div').eq(2).css('color','#00C5AC').siblings().css('color','#fff');
			}
			if(d==='/'){
				$('#yundian div').eq(3).css('color','#00C5AC').siblings().css('color','#fff');
			}
			for(var i = 0; i < yundian.length; i++) {
				yundian[i].index = i;
				yundian[i].onclick=function() {
					block.setFieldValue(yundian_string[this.index], 'var4');
					yun.innerHTML = block.getFieldValue('var4');
					$('#yundian div').eq(this.index).css('color','#00C5AC').siblings().css('color','#fff');
				}
			}
		    //更改常数与变量
			var numdian5 = document.getElementById('numdian5').getElementsByTagName('div');
			$("#dian5").html(block.getFieldValue('var5'));
			if(e==='常数'){
				$('#numdian5 div').eq(0).css('color','#00C5AC').siblings().css('color','#fff');
			}
			if(e==='变量'){
				$('#numdian5 div').eq(1).css('color','#00C5AC').siblings().css('color','#fff');
			}
			for(var i = 0; i < numdian5.length; i++) {
				numdian5[i].index = i;
				numdian5[i].onclick=function() {
					block.setFieldValue(numvar_string[this.index], 'var5');
					$("#dian5").html(block.getFieldValue('var5'));
					$('#numdian5 div').eq(this.index).css('color','#00C5AC').siblings().css('color','#fff');
				}
			}
			var cfn;
			cfn=block.getFieldValue('var6');
			$("#var6_val").css("background-size",cfn+"%"+" "+100+"%");
			$("#var6").html(cfn);
			$('#var6_val')[0].value=cfn;
			$('#var6_val').on('input propertychange',function(){
			 	cfn=$('#var6_val').val();
		        $("#var6_val").css("background-size",cfn+"%"+" "+100+"%");
		        $("#var6").html(cfn);
		        block.setFieldValue(cfn, 'var6');
		    });
			//取消设定的值，还原到原来的值
			$("#cancelbut").on('click',function(){
				block.setFieldValue(a, 'var1');
				block.setFieldValue(b, 'var2');
				block.setFieldValue(c, 'var3');
				block.setFieldValue(d, 'var4');
				block.setFieldValue(e, 'var5');
				block.setFieldValue(f, 'var6');
				$(".modal-dialog-bg").remove();
				$(".modal-dialog").remove();
			});
			//确定
			$("#sruebut").on('click',function(){
				$(".modal-dialog-bg").remove();
				$(".modal-dialog").remove();
			});
		}
	}
};
Blockly.JavaScript['tcr_r_opera'] = function(block) {
	var a = block.getFieldValue('var1');
	var b = block.getFieldValue('var2');
	var c = block.getFieldValue('var3');
	var d = block.getFieldValue('var4');
	var e = block.getFieldValue('var5');
	var f = block.getFieldValue('var6');
	var code;
	if(d=='×'){
		d='*';
	}
	if(d=='÷'){
		d='/';
	}
	if(b=='常数'){
		b='';
		if(e=='常数'){
			e='';
			code = "R[" + a + "]=" + b + c + d + e + f + ";";
		}
		if(e=='变量'){
			e='R['+f+']';
			code = "R[" + a + "]=" + b + c + d + e+ ";";
		}
	}
	if(b=='变量'){
		b='R['+c+']';
		if(e=='常数'){
			e='';
			code = "R[" + a + "]=" + b + d + e + f + ";";
		}
		if(e=='变量'){
			e='R['+f+']';
			code = "R[" + a + "]=" + b + d + e+ ";";
		}
	}
	return code;
};
//数字
Blockly.Blocks['tcr_number'] = {
	init: function() {
		this.appendDummyInput()
			.appendField(new TCR.FieldTextInput("1"), "n1");
		this.setInputsInline(true);
		this.setOutput(true, null);
		this.setColour(180);
		this.setTooltip('');
		this.setHelpUrl('');
	},
	onchange: function(ev) {
		if(ev.type == Blockly.Events.UI && ev.element == 'click' && this.id == ev.blockId) {
			var block = this;
			var dialog = new goog.ui.Dialog();
			dialog.setContent(
				'<div id="futitle" style="position: absolute;top: 12%;left:0%;display: none;">变量</div>'+
				
				'<div style="position:absolute;left: 45%;top: 33%;color:#fff;">数字<span id="var" style="color:#00FFFF;">1</span></div>'+
				'<input id="var_val" type="range" min="1" style="position:absolute;left: 22%;top: 41%;z-index:1000;"/>'+
				
				'<div id="cancelsruebut"><span id="cancelbut">取消</span><span id="sruebut">确定</span></div>'
			);
			dialog.setHasTitleCloseButton(false);
			dialog.setDraggable(false);
			dialog.setVisible(true);
			dialog.setDisposeOnHide(true);
			//保存当前的点位和速度的值
			var a = block.getFieldValue('n1');
			
			//设置变量
			var ccn;
			ccn=block.getFieldValue('n1');
			$("#var_val").css("background-size",ccn+"%"+" "+100+"%");
			$("#var").html(ccn);
			$('#var_val')[0].value=ccn;
			$('#var_val').on('input propertychange',function(){
			 	ccn=$('#var_val').val();
		        $("#var_val").css("background-size",ccn+"%"+" "+100+"%");
		        $("#var").html(ccn);
		        block.setFieldValue(ccn, 'n1');
		    });
			
			//取消设定的值，还原到原来的值
			$("#cancelbut").on('click',function(){
				block.setFieldValue(a, 'n1');
				$(".modal-dialog-bg").remove();
				$(".modal-dialog").remove();
			});
			//确定
			$("#sruebut").on('click',function(){
				$(".modal-dialog-bg").remove();
				$(".modal-dialog").remove();
			});
		}
	}
};
Blockly.JavaScript['tcr_number'] = function(block) {
	var b = block.getFieldValue('n1');
	var code = b
	return [code, Blockly.JavaScript.ORDER_ATOMIC];
};

//输出端口
Blockly.Blocks['tcr_do'] = {
	init: function() {
		this.appendDummyInput()
			.appendField("输出端口")
			.appendField(new TCR.FieldTextInput("1"), "do1")
			.appendField("=")
			.appendField(new TCR.FieldTextInput("ON"), "do2");
		this.setPreviousStatement(true, null);
		this.setNextStatement(true, null);
		this.setColour(180);
		this.setTooltip('');
		this.setHelpUrl('');
	},
	onchange: function(ev) {
		if(ev.type == Blockly.Events.UI && ev.element == 'click' && this.id == ev.blockId) {
			var block = this;
			var dialog = new goog.ui.Dialog();
			dialog.setContent(
				'<div id="futitle" style="position: absolute;top: 12%;left:0%;display: none;">端口</div>'+
				
				'<div style="position:absolute;left: 45%;top: 23%;color:#fff;">端口<span id="var1" style="color:#00FFFF;">1</span></div>'+
				'<input id="var1_val" type="range" min="1" max="16" style="position:absolute;left: 22%;top: 31%;z-index:1000;"/>'+
				
				'<div style="position:absolute;left: 45%;top: 36%;color:#fff;display: none;">开关<span id="swit" style="color:#00FFFF;">ON</span></div>'+
				'<div id ="swtichdian" style="display:block; width: 70%; height: 70%; position: absolute; left: 29%; top: 28%; background: transparent;">' +
				'<div style="font-size: 32px;position: absolute; left: 12%; top: 18%; width: 15%; height: 15%;text-align: center;z-index:999;">ON</div>' +
				'<div style="font-size: 32px;position: absolute; left: 30%; top: 18%; width: 15%; height: 15%;text-align: center;z-index:999;">OFF</div>' +
				'</div>' +
				
				'<div id="cancelsruebut"><span id="cancelbut">取消</span><span id="sruebut">确定</span></div>'
			);
			dialog.setHasTitleCloseButton(false);
			dialog.setDraggable(false);
			dialog.setVisible(true);
			dialog.setDisposeOnHide(true);
			//保存当前的点位和速度的值
			var a = block.getFieldValue('do1');
			var b = block.getFieldValue('do2');
			//设置端口变量
			var ccn;
			ccn=block.getFieldValue('do1');
			$("#var1_val").css("background-size",ccn*6.25+"%"+" "+100+"%");
			$("#var1").html(ccn);
			$('#var1_val')[0].value=ccn;
			$('#var1_val').on('input propertychange',function(){
			 	ccn=$('#var1_val').val();
		        $("#var1_val").css("background-size",ccn*6.25+"%"+" "+100+"%");
		        $("#var1").html(ccn);
		        block.setFieldValue(ccn, 'do1');
		    });
			//设置开关
			var swtichdian = document.getElementById('swtichdian').getElementsByTagName('div');
			var swit = document.getElementById('swit');
			swit.innerHTML = block.getFieldValue('do2');
			if(b==='ON'){
				$('#swtichdian div').eq(0).css('color','#00C5AC').siblings().css('color','#fff');
			}
			if(b==='OFF'){
				$('#swtichdian div').eq(1).css('color','#00C5AC').siblings().css('color','#fff');
			}
			for(var i = 0; i < swtichdian.length; i++) {
				swtichdian[i].index = i;
				swtichdian[i].onclick=function() {
					block.setFieldValue(swtichdian_string[this.index], 'do2');
					swit.innerHTML = block.getFieldValue('do2');
					$('#swtichdian div').eq(this.index).css('color','#00C5AC').siblings().css('color','#fff');
				}
			}
		   
			//取消设定的值，还原到原来的值
			$("#cancelbut").on('click',function(){
				block.setFieldValue(a, 'do1');
				block.setFieldValue(b, 'do2');
				$(".modal-dialog-bg").remove();
				$(".modal-dialog").remove();
			});
			//确定
			$("#sruebut").on('click',function(){
				$(".modal-dialog-bg").remove();
				$(".modal-dialog").remove();
			});
		}
	}
};
Blockly.JavaScript['tcr_do'] = function(block) {
	var b = block.getFieldValue('do1');
	var c = block.getFieldValue('do2');
	var code = "DO[" + b + "]=" + c + ";";
	return code;
};
//手抓激活
Blockly.Blocks['tcr_active'] = {
	init: function() {
		this.appendDummyInput()
			.appendField("手抓激活");
		this.setPreviousStatement(true, null);
		this.setNextStatement(true, null);
		this.setColour(40);
		this.setTooltip('');
		this.setHelpUrl('');
	}
};
Blockly.JavaScript['tcr_active'] = function(block) {
	var code = "ActiveGripper , LBL [1];";
	return code;
};
//手抓参数
Blockly.Blocks['tcr_gripper'] = {
	init: function() {
		this.appendDummyInput()
			.appendField("手抓开口到")
			.appendField(new TCR.FieldTextInput("0"), "gri1")
			.appendField("%")
			.appendField("速度")
			.appendField(new TCR.FieldTextInput("1"), "gri2")
			.appendField("%")
			.appendField("力矩")
			.appendField(new TCR.FieldTextInput("1"), "gri3")
			.appendField("%");
		this.setPreviousStatement(true, null);
		this.setNextStatement(true, null);
		this.setColour(40);
		this.setTooltip('');
		this.setHelpUrl('');
	},
	onchange: function(ev) {
		if(ev.type == Blockly.Events.UI && ev.element == 'click' && this.id == ev.blockId) {
			var block = this;
			var dialog = new goog.ui.Dialog();
			dialog.setContent(
				'<div id="futitle" style="position: absolute;top: 12%;left:0%;display: none;">手抓参数</div>'+
				
				'<div style="position:absolute;left: 48%;top: 25%;color:#fff;">开口<span id="posit" style="color:#00FFFF;">0</span><span style="color:#00FFFF;">%</span></div>'+
				'<input id="gripper_position" type="range" style="position:absolute;left: 22%;top: 33%;"/>'+
				
				'<div style="position:absolute;left: 48%;top: 40%;color:#fff;">速度<span id="speed" style="color:#00FFFF;">1</span><span style="color:#00FFFF;">%</span></div>'+
				'<input id="gripper_speed" type="range" min="1" style="position:absolute;left: 22%;top: 48%;"/>'+
				
				'<div style="position:absolute;left: 48%;top: 55%;color:#fff;">力矩<span id="force" style="color:#00FFFF;">1</span><span style="color:#00FFFF;">%</span></div>'+
				'<input id="gripper_force" type="range" min="1" style="position:absolute;left: 22%;top: 63%;"/>'+
				
				'<div id="cancelsruebut"><span id="cancelbut">取消</span><span id="sruebut">确定</span></div>'
			);
			dialog.setHasTitleCloseButton(false);
			dialog.setDraggable(false);
			dialog.setVisible(true);
			dialog.setDisposeOnHide(true);
			//保存当前的点位和速度的值
			var a = block.getFieldValue('gri1');
			var b = block.getFieldValue('gri2');
			var c = block.getFieldValue('gri3');
			//设置开口位置
			var can;
			can=block.getFieldValue('gri1');
			$("#gripper_position").css("background-size",can+"%"+" "+100+"%");
			$("#posit").html(can);
			$('#gripper_position')[0].value=can;
			$('#gripper_position').on('input propertychange',function(){
			 	can=$('#gripper_position').val();
		        $("#gripper_position").css("background-size",can+"%"+" "+100+"%");
		        $("#posit").html(can);
		        block.setFieldValue(can, 'gri1');
		    });
			//设置速度
			var cbn;
			cbn=block.getFieldValue('gri2');
			$("#gripper_speed").css("background-size",cbn+"%"+" "+100+"%");
			$("#speed").html(cbn);
			$('#gripper_speed')[0].value=cbn;
			$('#gripper_speed').on('input propertychange',function(){
			 	cbn=$('#gripper_speed').val();
		        $("#gripper_speed").css("background-size",cbn+"%"+" "+100+"%");
		        $("#speed").html(cbn);
		        block.setFieldValue(cbn, 'gri2');
		    });
		    //设置力矩
			var ccn;
			ccn=block.getFieldValue('gri3');
			$("#gripper_force").css("background-size",ccn+"%"+" "+100+"%");
			$("#force").html(ccn);
			$('#gripper_force')[0].value=ccn;
			$('#gripper_force').on('input propertychange',function(){
			 	ccn=$('#gripper_force').val();
		        $("#gripper_force").css("background-size",ccn+"%"+" "+100+"%");
		        $("#force").html(ccn);
		        block.setFieldValue(ccn, 'gri3');
		    });
			//取消设定的值，还原到原来的值
			$("#cancelbut").on('click',function(){
				block.setFieldValue(a, 'gri1');
				block.setFieldValue(b, 'gri2');
				block.setFieldValue(c, 'gri3');
				$(".modal-dialog-bg").remove();
				$(".modal-dialog").remove();
			});
			//确定
			$("#sruebut").on('click',function(){
				$(".modal-dialog-bg").remove();
				$(".modal-dialog").remove();
			});
			
		}
	}
};
Blockly.JavaScript['tcr_gripper'] = function(block) {
	var bb = block.getFieldValue('gri1')*2.55;
	var b=bb.toFixed(0); 
	var cc = block.getFieldValue('gri2')*2.55;
	var c=cc.toFixed(0); 
	var dd = block.getFieldValue('gri3')*2.55;
	var d=dd.toFixed(0); 
	var code = "SetGripper " + b + " " + c + " " + d + ";";
	return code;
};
//手抓开
Blockly.Blocks['tcr_gripper_open'] = {
	init: function() {
		this.appendDummyInput()
			.appendField("手抓开")
			.appendField("速度")
			.appendField(new TCR.FieldTextInput("1"), "open1")
			.appendField("%")
			.appendField("力矩")
			.appendField(new TCR.FieldTextInput("1"), "open2")
			.appendField("%");
		this.setPreviousStatement(true, null);
		this.setNextStatement(true, null);
		this.setColour(40);
		this.setTooltip('');
		this.setHelpUrl('');
	},
	onchange: function(ev) {
		if(ev.type == Blockly.Events.UI && ev.element == 'click' && this.id == ev.blockId) {
			var block = this;
			var dialog = new goog.ui.Dialog();
			dialog.setContent(
				'<div id="futitle" style="position: absolute;top: 12%;left:0%;display: none;">手抓参数</div>'+
				
				'<div style="position:absolute;left: 48%;top: 25%;color:#fff;">速度<span id="speed" style="color:#00FFFF;">1</span><span style="color:#00FFFF;">%</span></div>'+
				'<input id="gripper_speed" type="range" min="1" style="position:absolute;left: 22%;top: 33%;"/>'+
				
				'<div style="position:absolute;left: 48%;top: 40%;color:#fff;">力矩<span id="force" style="color:#00FFFF;">1</span><span style="color:#00FFFF;">%</span></div>'+
				'<input id="gripper_force" type="range" min="1" style="position:absolute;left: 22%;top: 48%;"/>'+
				
				'<div id="cancelsruebut"><span id="cancelbut">取消</span><span id="sruebut">确定</span></div>'
			);
			dialog.setHasTitleCloseButton(false);
			dialog.setDraggable(false);
			dialog.setVisible(true);
			dialog.setDisposeOnHide(true);
			//保存当前的点位和速度的值
			var a = block.getFieldValue('open1');
			var b = block.getFieldValue('open2');

			//设置速度
			var cbn;
			cbn=block.getFieldValue('open1');
			$("#gripper_speed").css("background-size",cbn+"%"+" "+100+"%");
			$("#speed").html(cbn);
			$('#gripper_speed')[0].value=cbn;
			$('#gripper_speed').on('input propertychange',function(){
			 	cbn=$('#gripper_speed').val();
		        $("#gripper_speed").css("background-size",cbn+"%"+" "+100+"%");
		        $("#speed").html(cbn);
		        block.setFieldValue(cbn, 'open1');
		    });
		    //设置力矩
			var ccn;
			ccn=block.getFieldValue('open2');
			$("#gripper_force").css("background-size",ccn+"%"+" "+100+"%");
			$("#force").html(ccn);
			$('#gripper_force')[0].value=ccn;
			$('#gripper_force').on('input propertychange',function(){
			 	ccn=$('#gripper_force').val();
		        $("#gripper_force").css("background-size",ccn+"%"+" "+100+"%");
		        $("#force").html(ccn);
		        block.setFieldValue(ccn, 'open2');
		    });
			//取消设定的值，还原到原来的值
			$("#cancelbut").on('click',function(){
				block.setFieldValue(a, 'open1');
				block.setFieldValue(b, 'open2');
				$(".modal-dialog-bg").remove();
				$(".modal-dialog").remove();
			});
			//确定
			$("#sruebut").on('click',function(){
				$(".modal-dialog-bg").remove();
				$(".modal-dialog").remove();
			});
			
		}
	}
};
Blockly.JavaScript['tcr_gripper_open'] = function(block) {
	var bb = block.getFieldValue('open1')*2.55;
	var b=bb.toFixed(0); 
	var cc = block.getFieldValue('open2')*2.55;
	var c=cc.toFixed(0); 
	var code = "SetGripper 0 " + b + " " + c + ";";
	return code;
};
//手抓关
Blockly.Blocks['tcr_gripper_colse'] = {
	init: function() {
		this.appendDummyInput()
			.appendField("手抓关")
			.appendField("速度")
			.appendField(new TCR.FieldTextInput("1"), "colse1")
			.appendField("%")
			.appendField("力矩")
			.appendField(new TCR.FieldTextInput("1"), "colse2")
			.appendField("%");
		this.setPreviousStatement(true, null);
		this.setNextStatement(true, null);
		this.setColour(40);
		this.setTooltip('');
		this.setHelpUrl('');
	},
	onchange: function(ev) {
		if(ev.type == Blockly.Events.UI && ev.element == 'click' && this.id == ev.blockId) {
			var block = this;
			var dialog = new goog.ui.Dialog();
			dialog.setContent(
				'<div id="futitle" style="position: absolute;top: 12%;left:0%;display: none;">手抓参数</div>'+
				
				'<div style="position:absolute;left: 48%;top: 25%;color:#fff;">速度<span id="speed" style="color:#00FFFF;">1</span><span style="color:#00FFFF;">%</span></div>'+
				'<input id="gripper_speed" type="range" min="1" style="position:absolute;left: 22%;top: 33%;"/>'+
				
				'<div style="position:absolute;left: 48%;top: 40%;color:#fff;">力矩<span id="force" style="color:#00FFFF;">1</span><span style="color:#00FFFF;">%</span></div>'+
				'<input id="gripper_force" type="range" min="1" style="position:absolute;left: 22%;top: 48%;"/>'+
				
				'<div id="cancelsruebut"><span id="cancelbut">取消</span><span id="sruebut">确定</span></div>'
			);
			dialog.setHasTitleCloseButton(false);
			dialog.setDraggable(false);
			dialog.setVisible(true);
			dialog.setDisposeOnHide(true);
			//保存当前的点位和速度的值
			var a = block.getFieldValue('colse1');
			var b = block.getFieldValue('colse2');

			//设置速度
			var cbn;
			cbn=block.getFieldValue('colse1');
			$("#gripper_speed").css("background-size",cbn+"%"+" "+100+"%");
			$("#speed").html(cbn);
			$('#gripper_speed')[0].value=cbn;
			$('#gripper_speed').on('input propertychange',function(){
			 	cbn=$('#gripper_speed').val();
		        $("#gripper_speed").css("background-size",cbn+"%"+" "+100+"%");
		        $("#speed").html(cbn);
		        block.setFieldValue(cbn, 'colse1');
		    });
		    //设置力矩
			var ccn;
			ccn=block.getFieldValue('colse2');
			$("#gripper_force").css("background-size",ccn+"%"+" "+100+"%");
			$("#force").html(ccn);
			$('#gripper_force')[0].value=ccn;
			$('#gripper_force').on('input propertychange',function(){
			 	ccn=$('#gripper_force').val();
		        $("#gripper_force").css("background-size",ccn+"%"+" "+100+"%");
		        $("#force").html(ccn);
		        block.setFieldValue(ccn, 'colse2');
		    });
			//取消设定的值，还原到原来的值
			$("#cancelbut").on('click',function(){
				block.setFieldValue(a, 'colse1');
				block.setFieldValue(b, 'colse2');
				$(".modal-dialog-bg").remove();
				$(".modal-dialog").remove();
			});
			//确定
			$("#sruebut").on('click',function(){
				$(".modal-dialog-bg").remove();
				$(".modal-dialog").remove();
			});
			
		}
	}
};
Blockly.JavaScript['tcr_gripper_colse'] = function(block) {
	var bb = block.getFieldValue('colse1')*2.55;
	var b=bb.toFixed(0); 
	var cc = block.getFieldValue('colse2')*2.55;
	var c=cc.toFixed(0); 
	var code = "SetGripper 255 " + b + " " + c + ";";
	return code;
};

var numdian_string = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10'];
var numdian_sec_string = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '.'];
var fuhaodian_string = ['=', '≠', '>', '<'];
var swtichdian_string = ['ON', 'OFF'];
var didodian_string = ['DI', 'DO'];
var yundian_string = ['+', '-', '×', '÷'];
var numvar_string = ['常数', '变量'];